package com.yanqu.road.server.cmd.activity.fivehero;

import com.yanqu.road.entity.activity.fivehero.FiveHeroConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.FiveHeroProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.fivehero.FiveHeroModule;

import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.fivehero.FiveHeroMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = GameProtocol.S_FIVEHERO_CROSS_GET_PATRONS, desc = "")
public class CrossFiveHeroGetPatronsCmd extends AbstractCommand {

    private FiveHeroProto.CrossFiveHeroUpdatePatronsReqMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = FiveHeroProto.CrossFiveHeroUpdatePatronsReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        FiveHeroConfig config = FiveHeroMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        FiveHeroProto.CrossFiveHeroUpdatePatronsRespMsg.Builder builder = FiveHeroProto.CrossFiveHeroUpdatePatronsRespMsg.newBuilder();
        builder.setActivityId(reqMsg.getActivityId());
        List<Long> outLineUserIdList = new ArrayList<>();
        for(long userId : reqMsg.getUserIdListList()){
            GamePlayer gamePlayer = GamePlayerMgr.getOnlinePlayer(userId);
            if(gamePlayer != null){
                FiveHeroProto.CrossFiveHeroUpdatePatronsUserMsg.Builder userMsg = gamePlayer.getModule(FiveHeroModule.class).getSyncPatronsMsg();
                builder.addUsers(userMsg);
            }else{
                outLineUserIdList.add(userId);
            }
        }
        //一点一点去拉
        if(outLineUserIdList.size() > 0){
            builder.addAllUsers(FiveHeroMgr.getSyncPatronsMsg(outLineUserIdList));
        }
        int groupId = reqMsg.getGroupId();
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_FIVE_HERO_SYNC_PATRONS, builder,0,reqMsg.getActivityId(),groupId);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }
    }
}
